//
//  LoginViewController.m
//  soccer
//
//  Created by 上官千城 on 15/9/10.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OLoginViewController.h"

@interface OLoginViewController ()

@end

@implementation OLoginViewController

-(void) layout
{
    //背景
    UIImageView *backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    backView.image = [UIImage imageNamed:@"登陆注册@2x"];
    [self.view addSubview:backView];
    //用户名
    self.userName = [[UITextField alloc]initWithFrame:CGRectMake(40, 150, WIDTH - 80, 30)];
    self.userName.borderStyle = UITextBorderStyleRoundedRect;
    self.userName.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.userName.delegate = self;
    self.userName.placeholder = @"请输入用户名";
    [self.view addSubview:self.userName];
    //密码
    self.password1 = [[UITextField alloc]initWithFrame:CGRectMake(self.userName.frame.origin.x, self.userName.frame.origin.y + self.userName.frame.size.height + 30, self.userName.frame.size.width, self.userName.frame.size.height)];
    self.password1.delegate = self;
    self.password1.placeholder = @"输入密码";
    self.password1.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.password1.borderStyle = UITextBorderStyleRoundedRect;
    self.password1.textColor = [UIColor blackColor];
    self.password1.secureTextEntry = YES;
    [self.view addSubview:self.password1];
    //登陆
//    UIImageView *loginImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"登陆1@2x"] highlightedImage:[UIImage imageNamed:@"登陆"]];

    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    [login addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    login.frame = CGRectMake( 30 , self.password1.frame.origin.y + self.password1.frame.size.height + 60, WIDTH / 3, 35 * Proportion);
    [login setImage:[UIImage imageNamed:@"登陆1@2x"] forState:UIControlStateNormal];
    [login setImage:[UIImage imageNamed:@"登陆"] forState:UIControlStateHighlighted];
//    login.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:login];
    //注册
    UIButton *Registered = [UIButton buttonWithType:UIButtonTypeCustom];
    Registered.frame = CGRectMake(WIDTH - login.frame.size.width - 30, login.frame.origin.y, login.frame.size.width, login.frame.size.height);
    [Registered setImage:[UIImage imageNamed:@"注册1"] forState:UIControlStateNormal];
    [Registered setImage:[UIImage imageNamed:@"注册"] forState:UIControlStateHighlighted];
//    Registered.backgroundColor = [UIColor orangeColor];
//    [Registered setTitle:@"注册" forState:UIControlStateNormal];
    [Registered addTarget:self action:@selector(registereAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Registered];
    //忘记密码
    UIButton *forget = [UIButton buttonWithType:UIButtonTypeCustom];
    forget.frame = CGRectMake(self.password1.frame.origin.x+self.password1.frame.size.width - 70, self.password1.frame.origin.y + self.password1.frame.size.height+5, 90, 20);
    [forget addTarget:self action:@selector(forgetButton) forControlEvents:UIControlEventTouchUpInside];
    [forget setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.view addSubview:forget];
    
}




-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    [self.view endEditing:YES];

}

- (void) initNetHandle
{
    self.loginLogic = [[OLoginLogic alloc]initWithCallBack:@selector(loginCallBack:) for:self];
    self.enterAppLogic = [[OEnterApp alloc]initWithCallBack:@selector(enterAppCallBack:) for:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self layout];
    [self initNetHandle];
}

-(void) loginCallBack:(OBaseLogic*)logicObj
{
    self.enterAppLogic.token = self.loginLogic.token;
    self.enterAppLogic.sign = self.loginLogic.sign;
    [self.enterAppLogic request];
    NSLog(@"enterApp start....");
}

-(void) enterAppCallBack:(OBaseLogic*)logicObj
{
    NSString *userName = [NSString stringWithFormat:@"o_%ld",self.enterAppLogic.userId];
    NSLog(@"----->%@",userName);
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:userName password:self.password1.text completion:^(NSDictionary *loginInfo, EMError *error) {
        
        if ([userName isEqualToString:[loginInfo objectForKey:@"username"]]) {
            
            NSLog(@"环信登陆成功");
        }
        
        
    } onQueue:nil];
    
    
    
    OUserInfo* info = [OUserInfo getInfo];
    if (!info) {
        info = [[OUserInfo alloc] init];
    }
    info.token = self.loginLogic.token;
    info.sign = self.loginLogic.sign;
    info.userId = self.enterAppLogic.userId;
    info.userName = self.loginLogic.account;
    info.region = self.enterAppLogic.region;
    info.sign = self.enterAppLogic.sign;
    info.nickName = self.enterAppLogic.nickName;
    info.sex = self.enterAppLogic.sex;
    info.icon = self.enterAppLogic.icon;
    [OUserInfo setInfo:info];
    //[info update];
    
    NSString *imAccount = [NSString stringWithFormat:@"o_%ld",self.enterAppLogic.userId];
    [[OImManager shareManager] asynLoginWithUserName:imAccount password:self.loginLogic.pwd];
    //TODO:[self.loginDelegate setRootViewControllerLog];
}

//登陆
-(void)loginAction
{

    if ([self.userName.text isEqualToString:@""]|| [self.password1.text isEqualToString:@""]) {
        [UIUtils showAlert:@"提示" message:@"用户名密码不能为空" left:@"确定" right:nil delegate:nil];
        return;
    }
    
    self.loginLogic.account = self.userName.text;
    self.loginLogic.pwd = self.password1.text;
    [self.loginLogic request];
 
}


//忘记密码
-(void)forgetButton
{
    
}


//注册
-(void)registereAction
{
    ORegisteredController *view = [[ORegisteredController alloc]init];
    view.myDelegate = self;
    [self presentViewController:view animated:YES completion:^{
    }];

}

//注册协议方法
-(void)setRootViewControllerReg
{
    [self.loginDelegate setRootViewControllerLog];
    OConsultViewController *view = [[OConsultViewController alloc]init];
    [self presentViewController:view animated:YES completion:^{
        self.view.window.rootViewController = view;
        
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.userName resignFirstResponder];
    [self.password1 resignFirstResponder];
    return YES;
}

@end
